package com.longrise.android.byjk.plugins.hra.hratabsecond;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.hra.hratabfirst.ShareMsgDialogFragment;
import com.longrise.android.byjk.plugins.hra.hratabthird.EndorseRefreshEvent;
import com.longrise.common.base.BaseActivity2;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2018/1/11.
 * 预约详情
 */

public class MineAppointDetailActivity extends BaseActivity2<MineOrderDetailPresenter> implements MineOrderDetailContract.View, View.OnClickListener {

    public static final String TAG = "MineAppointDetailActivity";
    public static final String MINEORDERID = "orderid";
    public static final String SHOWFORKBACK = "showforkback";
    public static final String ENDORSEBACK = "endorseback";
    public static final String HRASERVICEBACK = "endorseback";
    private TextView mOrderPoint;
    private TextView mOrderArea;
    private TextView mOrderTel;
    private TextView mOrderDate;
    private TextView mOrderTime;
    private TextView mPrderName;
    private TextView mOrderSex;
    private TextView mOrderCard;
    private TextView mOrderPhone;
    private TextView oOrderNum;
    private TextView mOrderPrice;
    private ImageView iv_back;
    private Button mBtnSend;
    private String orderId;
    private String storename;
    private String address;
    private String examinedate;
    private String timeslot;
    private String username;
    private String machinemodel;
    private String sex;
    private boolean isEndorsekBack;
    private boolean hraServiceBack;
    private boolean isShowForkBack;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_mine_appoint_detail;
    }

    @Override
    public void initView() {
        mOrderPoint = (TextView) findViewById(R.id.tv_order_appoint_point);
        mOrderArea = (TextView) findViewById(R.id.tv_order_appoint_area);
        mOrderTel = (TextView) findViewById(R.id.tv_order_appoint_tel);
        mOrderDate = (TextView) findViewById(R.id.tv_order_appoint_date);
        mOrderTime = (TextView) findViewById(R.id.tv_order_appoint_time);
        mPrderName = (TextView) findViewById(R.id.tv_order_appoint_name);
        mOrderSex = (TextView) findViewById(R.id.tv_order_appoint_sex);
        mOrderCard = (TextView) findViewById(R.id.tv_order_appoint_card);
        mOrderPhone = (TextView) findViewById(R.id.tv_order_appoint_phone);
        oOrderNum = (TextView) findViewById(R.id.tv_order_appoint_num);
        mOrderPrice = (TextView) findViewById(R.id.tv_order_appoint_price);
        iv_back = (ImageView) findViewById(R.id.iv_order_appoint_back);
        mBtnSend = (Button) findViewById(R.id.btn_send_customer);
        setToolbarVisible(false);
        getExtraDatas();
        initEvent();
        mPresenter.getOrderDetailData(orderId);
    }

    public void getExtraDatas() {
        Intent intent = getIntent();
        orderId = intent.getStringExtra(MINEORDERID);
        isShowForkBack = intent.getBooleanExtra(SHOWFORKBACK, false);
        isEndorsekBack = intent.getBooleanExtra(ENDORSEBACK, false);
        hraServiceBack = intent.getBooleanExtra(HRASERVICEBACK, false);
        if (isShowForkBack||isEndorsekBack||hraServiceBack) {
            iv_back.setImageResource(R.drawable.by_ic_close_black);
        }

    }


    public void initEvent() {
        iv_back.setOnClickListener(this);
        mBtnSend.setOnClickListener(this);
    }


    //设置数据到View
    public void setData2View(EntityBean bean) {
        storename = bean.getString("storename");
        address = bean.getString("address");
        String storephone = bean.getString("storephone");
        examinedate = bean.getString("examinedate");
        timeslot = bean.getString("timeslot");
        username = bean.getString("username");
        sex = bean.getString("sex");
        String cardno = bean.getString("cardno");
        String usermobile = bean.getString("usermobile");
        String orderid = bean.getString("orderid");
        String price = bean.getString("total_fee");
        machinemodel = bean.getString("machinemodel");
        int checkstate = bean.getInt("checkstate");

        mOrderPoint.setText(storename);
        mOrderArea.setText(address);
        mOrderTel.setText(storephone);
        mOrderDate.setText(examinedate);
        mOrderTime.setText(timeslot);
        mPrderName.setText(username);
        mOrderSex.setText(sex);
        mOrderCard.setText(cardno);
        mOrderPhone.setText(usermobile);
        oOrderNum.setText(orderid);
        mOrderPrice.setText("¥" + price);

        //未检测
        if(1 == checkstate){
            mBtnSend.setVisibility(View.VISIBLE);
        }else if(3 == checkstate){
            mBtnSend.setVisibility(View.GONE);
        }

    }

    @Override
    public void onToolbarBackClick() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_order_appoint_back:
                EndorseRefreshEvent event = new EndorseRefreshEvent();
                event.setRefresh(true);
                event.setClose(true);
                EventBus.getDefault().post(event);
                finish();
                break;
            //点击发送给客户
            case R.id.btn_send_customer:
                toShowShareDialog();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(isShowForkBack||isEndorsekBack||hraServiceBack){
                EndorseRefreshEvent event1 = new EndorseRefreshEvent();
                event1.setRefresh(true);
                event1.setClose(true);
                EventBus.getDefault().post(event1);
                finish();
            }else {
                finish();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 分享给客户
     */
    private void toShowShareDialog() {
        String title = "";
        if ("男".equals(sex)) {
            title = username + "先生，这是为您预约的体检信息，请妥善保存，按时前往";
        } else if ("女".equals(sex)) {
            title = username + "女士，这是为您预约的体检信息，请妥善保存，按时前往";
        }

        String fwd = storename;
        String xxdz = address;
        String jcrq = examinedate;
        String yysj = timeslot;
        String jcjx = machinemodel;
        ShareMsgDialogFragment dialogFragment1 = ShareMsgDialogFragment.newInstance(title, fwd, xxdz, jcrq, yysj, jcjx);
        dialogFragment1.show(getSupportFragmentManager(), "sharemsgdialog");
    }

    @Override
    public void refreshData(EntityBean bean) {
        EntityBean result = bean.getBean("result");
        setData2View(result);
    }

    @Override
    public void setRefreshing(boolean isRefreshing) {

    }
}
